! /Users/lamfin3/BSV/22/code/eval/CEoos.f90
!       update the first stage out-of-sample results -- used to select the optimal model.
!   For each year in the oos2 period (using data to the start of that year), write the sampling distribution of 
!       the CE under each of the 14 values of gamma* for the model.
USE SVRGN_INT
IMPLICIT NONE
!
! Step 1 in creating the cvertainty equivalent plots and summary stats.
!    For one model and 1 gamma, real the NBS bootstrap returns and compute
!        the CE rteurn for each draw.  Each draw has 3 vectors of returns:
!            the BSV optimal, the Equally-wtd, and Value-wtd (in that order).
!    Print out the means and std dev, and
!        write the all NBS CE returns to disk for this case.
!
DOUBLE PRECISION, DIMENSION(1000,3) :: ret
DOUBLE PRECISION, DIMENSION(10000,50,3) :: CE
DOUBLE PRECISION, DIMENSION(10000) :: FOSO,OSO
DOUBLE PRECISION, DIMENSION(50,3) :: s,ss
DOUBLE PRECISION, DIMENSION(3) :: FOU,SFOU
DOUBLE PRECISION :: one,gamma,zero,maxret
INTEGER :: mdate(1000),T,i,j,NBS,imy,imj,imo,iostat,errcod,ib,jp,ip2,ipq1,med,ipq3,ip9,ip01
INTEGER :: iunit,im,gstar,ISJ,BR,igarb,iy,TOOS1,TOOS2,NYOOS2,ny,OOSM
OPEN(unit= 21, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g2/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 22, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g3/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 23, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g4/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 24, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g5/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 25, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g6/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 26, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g7/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 27, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g8/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 28, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g9/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 29, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g10/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 30, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g11/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 31, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g12/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 32, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g13/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 33, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g16/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 34, file='/Volumes/BSV2/data/output/bootstrap/2022/M39/g22/rets.dat',STATUS ='OLD',iostat= errcod)
OPEN(unit= 128, file='oos1/g8/M39.out')
gamma=8.0d0
one=1.0d0
zero=0.0d0
NBS=10000
ip01=.01*NBS
ip2=.025*NBS
ipq1=.25*NBS
med=.5*NBS
ipq3=.75*NBS
ip9=.975*NBS
! T=528                    ! 528 through 2018  564 through 2021.
T=564                    ! 528 through 2018  564 through 2021.
TOOS1=180                ! First oos period
TOOS2=T-TOOS1
NYOOS2=TOOS2/12
iunit=20
imy=12; imj=0; imo=0
DO im=1,14               !        <------------No of files (model / gamma combos).
   gstar=im+1               !        <------------gamma-star used to create optimal portfolios.
   IF (gstar == 14) gstar=16
   IF (gstar == 15) gstar=22
      WRITE (128,6990) gstar; 6990 format (/'    gamma-star ',i2)
   PRINT 6990, gstar
      iunit=iunit+1
   DO ny=1,50
      DO j=1,3
         s(ny,j)=zero
         ss(ny,j)=zero
      END DO
   END DO
   DO ib=1,NBS 
      BR=0
      maxret=-one
      DO i=1,T
         READ(iunit,117) MDATE(i),(ret(i,j),j=1,3)   ! optimal, EW, VW
                 117 FORMAT(1X,I8,3(2X,F12.8))                                        ! Recall that in the BSV structure
                                                                                      ! we have leading returns so that
                                                                                      ! the returns on 19691231 are for January 1970, 
                                                                                      ! etc.
          maxret=MAX(ret(i,1),maxret)
      END DO
!
!  All returns for the entire OOS period are read in.
!
      DO ny=1,NYOOS2
         ISJ=1
         DO j=1,3
            SFOU(j)=zero
         END DO
         OOSM=TOOS1+(ny-1)*12
         DO i=1,OOSM
            DO j=1,3
               IF (ret(i,j) <= -1.d0) THEN
                  CE(ib,ny,j)=-one
                  ISJ=2
               ELSE 
                  SFOU(j) = SFOU(j) +((one+ret(i,j))**(one-gamma))/(one-gamma)
               END IF
            END DO
         END DO
         DO j=ISJ,3
            FOU(j)=SFOU(j)/OOSM
         END DO
         DO j=ISJ,3
            CE(ib,ny,j)=(FOU(j)*(one-gamma))**(one/(one-gamma))-one
         END DO
         IF (CE(ib,ny,1) > one) THEN
              PRINT 5654, ib,maxret; 5654 format (' blow up: draw ',i6,' maxret: ',f16.7)
         END IF
         DO j=1,3
            s(ny,j)=s(ny,j)+CE(ib,ny,j)
         END DO
      END DO
   END DO                !   Next bootstrap sample.
!
!  For one gamma, we have 10,000 bootstrap samples at this point.
!
   DO ny=1,NYOOS2
      DO j=1,3
         s(ny,j)=s(ny,j)/NBS
      END DO
      DO ib=1,NBS
         DO j=1,3
            ss(ny,j)=ss(ny,j)+(CE(ib,ny,j)-s(ny,j))*(CE(ib,ny,j)-s(ny,j))
         END DO
      END DO
      DO j=1,3
         ss(ny,j)=sqrt(ss(ny,j)/(NBS-one))
         DO ib=1,NBS
            FOSO(ib)=CE(ib,ny,j)
         END DO
         CALL SVRGN(FOSO,OSO)
         WRITE (128,6656) s(ny,j),ss(ny,j),oso(ip01),oso(ip2),oso(ipq1),oso(med),oso(ipq3),oso(ip9)
                   6656 format (8(1x,f14.10))
      END DO
!       PRINT 6776, ny; 6776 format (' ny: ',i2)
!       PRINT 6785, (s(ny,jp), jp=1,3); 6785 format (' Means: ',3(6x,f16.9))
!       PRINT 6786, (ss(ny,jp), jp=1,3); 6786 format (' Std Devs: ',3x,f16.9,2(6x,f16.9))
   END DO
END DO
STOP; END
